from datetime import timedelta
from celery.schedules import crontab
from celery import Celery

from kombu import Queue
from config import settings

# Celery settings
celery_app = Celery(__name__, broker=settings.redis, backend=settings.redis)

celery_app.conf.task_default_queue = "medium"
celery_app.conf.celeryd_max_tasks_per_child = 1000
celery_app.conf.task_create_missing_queues = True
celery_app.conf.celeryd_task_time_limit = 60 * 60 * 1  # 5 hour
celery_app.conf.broker_transport_options = {"visibility_timeout": 3600}  # one hour

celery_app.conf.task_queues = (
    Queue("high"),
    Queue("medium"),
    Queue("low"),
    Queue("es"),
)

celery_app.conf.imports = [
    'task_utils.task_jobs',
    "task_utils.task_es"
]

# todo fixme
# CELERY ROUTES
# https://medium.com/@ffreitasalves/using-celery-with-multiple-queues-retries-and-scheduled-tasks-589fe9a4f9ba

celery_app.conf.task_routes = {
    "task_utils.task_jobs.job_join_act_contacts": {"queue": "high"},
    "task_utils.task_jobs.job_alumni_join_act": {"queue": "high"},
    "task_utils.task_jobs.job_update_alumni_completion_score": {"queue": "high"},
    "task_utils.task_jobs.job_institution_to_act": {"queue": "medium"},
    "task_utils.task_jobs.job_update_ranking": {"queue": "low"}
}

celery_app.conf.update(
    task_serializer='pickle',
    result_serializer='pickle',
    accept_content=['pickle']
)

celery_app.conf.update(task_track_started=True)


celery_app.conf.beat_schedule = {
    'check-undefined-registers': {
        'task': 'task_utils.task_jobs.job_check_orphan',
        'schedule': crontab(minute=0, hour='*/8'),  # 每周日检查一次
        'args': ()
    },
    'metric-institution-overview': {
        'task': 'task_utils.task_jobs.job_task_institution_overview',
        'schedule': crontab(minute=13, hour=1),  # 每天晚上11点13分执行一次
        'args': ()
    },
    'metric-activity_span': {
        'task': 'task_utils.task_jobs.job_task_update_activity_span',
        'schedule': crontab(minute=13, hour=3),  # 每天晚上3点13分执行一次
        'args': ()
    },
    'metric-activity-radar': {
        'task': 'task_utils.task_jobs.job_task_radar',
        'schedule': crontab(minute=13, hour=21, day_of_week="sat"),  # 每天晚上11点13分执行一次
        'args': ()
    },
    'metric-es-institution-overview': {
        'task': 'task_utils.task_es.job_task_institution_overview',
        'schedule': crontab(minute=0, hour='*/3'),  # 每天晚上11点13分执行一次
        'args': ()
    },
    'metric-es-institution-radar': {
        'task': 'task_utils.task_es.job_task_radar',
        'schedule': crontab(minute=0, hour='*/8'),  # 每天晚上11点13分执行一次
        'args': ()
    },
    'metric-es-institution-span': {
        'task': 'task_utils.task_es.job_task_update_activity_span',
        'schedule': crontab(minute=0, hour='*/8'),  # 每天晚上11点13分执行一次
        'args': ()
    },
    'metric-es-institution-toes': {
        'task': 'task_utils.task_jobs.job_task_update_dept_to_es',
        'schedule': crontab(minute=13, hour=4),  # 每天4点
        'args': ()
    },
    'metric-ranking': {
        'task': 'task_utils.task_es.job_update_ranking',
        'schedule': crontab(minute=3, hour=22),  # 每天晚上11点13分执行一次
        'args': ()
    },
    'metric-ranking-v2': {
        'task': 'task_utils.task_es.job_update_ranking_v2',
        'schedule': crontab(minute=10, hour=22),  # 每天晚上11点13分执行一次
        'args': ()
    },
    'metric-redis_ranking': {
        'task': 'task_utils.task_es.job_task_alumni_by_redis',
        'schedule': crontab(minute=3, hour=23, day_of_week="sat"),
        'args': ()
    },

}

